package com.example.controller;

import com.example.common.Result;
import com.example.entity.Article;
import com.example.entity.Course;
import com.example.service.ArticleService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
@RequestMapping("/article")
@RestController
public class ArticleController {
    @Resource
    ArticleService articleService;
    @GetMapping("/select")
    public Result getAll(@RequestParam(defaultValue = "1")Integer pageNum,
                         @RequestParam(defaultValue = "10") Integer pageSize,
                         Article article){
            PageInfo<Article> articlePageInfo = articleService.seleContent(pageNum,pageSize,article);
            return  Result.success(articlePageInfo);
    }
    @PostMapping("/add")
    public Result add(@RequestBody Article article){
        if(article.getTitle().length()>30){
            return  Result.error("标题字数超过限制");
        }
        if (article.getContent().length()>8000){
            return  Result.error("正文内容超过限制");
        }
            articleService.add(article);
            return  Result.success();
    }
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
            articleService.deleteByid(id);
            return  Result.success();
    }
    @PutMapping("/update")
    public Result update(@RequestBody Article article){
        if(article.getTitle().length()>30){
            return  Result.error("标题字数超过限制");
        }
        if (article.getContent().length()>8000){
            return  Result.error("正文内容超过限制");
        }
            articleService.update(article);
            return  Result.success();
    }
}
